package org.jeecg.modules.bigscreen.controller;


import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.bigscreen.service.BigScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 数据大屏
 * @Author: zhangjiaping
 * @Date:   2021-05-18
 * @Version: V1.0
 */
@Api(tags="数据大屏接口")
@RestController
@RequestMapping("/bigscreen")
@Slf4j
public class BigScreenController {

	@Autowired
	private BigScreenService bigScreenService;


	/**
	 * 获取住宿数据
	 *
	 * @return
	 */
	@AutoLog(value = "大屏数据-获取住宿数据")
	@ApiOperation(value="大屏数据-获取住宿数据", notes="大屏数据-获取住宿数据")
	@GetMapping(value = "/queryHotelData")
	public Result<?> queryHotelData() {

		try {
			return Result.OK(bigScreenService.getHotelData());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取住宿数据失败:"+error.getMessage());
		}

	}

	/**
	 * 获取进出人员数据
	 *
	 * @return
	 */
	@AutoLog(value = "大屏数据-获取进出人员数据")
	@ApiOperation(value="大屏数据-获取进出人员数据", notes="大屏数据-获取进出人员数据")
	@GetMapping(value = "/queryInOutData")
	public Result<?> queryInOutData() {

		try {
			return Result.OK(bigScreenService.getHotelData());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取住宿数据失败:"+error.getMessage());
		}

	}

	/**
	 * 各院系违纪房间个数统计
	 */
	@AutoLog(value = "各院系违纪房间个数统计")
	@ApiOperation(value="各院系违纪房间个数统计", notes="各院系违纪房间个数统计")
	@GetMapping(value = "/queryCollegeViolationInfo")
	public Result<?>queryCollegeViolationInfo(HttpServletRequest req){
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String selectedSchools = sysUser.getSelectedSchools();
		List<Map<String,Integer>> data = bigScreenService.queryCollegeViolationInfo(selectedSchools);
		return Result.OK(data);
	}
	/**
	 * 获取卫生检查数据
	 *
	 * @return
	 */
	@AutoLog(value = "大屏数据-获取卫生检查数据")
	@ApiOperation(value="大屏数据-获取卫生检查数据", notes="大屏数据-获取卫生检查数据")
	@GetMapping(value = "/queryCheckScoreAnalysis")
	public Result<?> queryCheckScoreAnalysis() {

		try {
//			return Result.OK(bigScreenService.getHotelData());
			return Result.OK(bigScreenService.getCheckScore());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取住宿数据失败:"+error.getMessage());
		}

	}

/*	@AutoLog(value = "大屏数据-电量统计")
	@ApiOperation(value="大屏数据-电量统计", notes="大屏数据-电量统计")
	@GetMapping(value = "/queryElectricityAnalysis")
	public Result<?> queryElectricityAnalysis() {

		try {
			return Result.OK(bigScreenService.getBuildingElectricityData());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取住宿数据失败:"+error.getMessage());
		}

	}*/
	@AutoLog(value = "大屏数据-卫生成绩检查通报")
	@ApiOperation(value="大屏数据-卫生成绩检查通报", notes="大屏数据-卫生成绩检查通报")
	@GetMapping(value = "/queryHealthScore")
	public Result<?> queryHealthScore() {

		try {
			return Result.OK(bigScreenService.queryHealthScore());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取卫生成绩检查数据失败:"+error.getMessage());
		}

	}
	/**
	 *   海康威视-宿管考勤-考勤结果
	 *
	 * @return
	 */
	@AutoLog(value = "海康威视-宿管考勤-考勤结果")
	@ApiOperation(value="海康威视-宿管考勤-考勤结果")
	@PostMapping(value = "/inOutStatistics")
	public Result<?> inOutStatistics() throws Exception {
		//路径
		ArtemisConfig.host = "192.168.100.46:443"; // artemis 网关服务器 ip 端口
		ArtemisConfig.appKey = "26971752"; // 秘钥 appkey
		ArtemisConfig.appSecret = "B1NXm12eZzRlcuaFX95f";// 秘钥 appSecret
		final String ARTEMIS_PATH = "/artemis";
		final String queryDcmsRecordCountApi = ARTEMIS_PATH +
				"/api/dcms/v1/openApiService/record/queryDcmsRecordCount";
		Map<String, String> path = new HashMap<String, String>(2) {
			{
				put("https://", queryDcmsRecordCountApi);//根据现场环境部署确认是 http 还是https
			}
		};
		String contentType = "application/json";
		// 传参
		JSONObject jsonBody = new JSONObject();
		jsonBody.put("date", LocalDate.now().minusDays(1).toString());
		jsonBody.put("ruleName", "山科大公寓管理考勤");
		jsonBody.put("dormName", "");
		//result : 这是返回的数据
		try {
			String result = ArtemisHttpUtil.doPostStringArtemis(path,
					jsonBody.toString(), null, null, contentType, null);
			JSONObject response = JSONObject.parseObject(result);
			int code = response.getIntValue("code");
			String msg = response.getString("msg");
			if (code != 0) {
				return Result.error(msg);
			}
			JSONObject data = response.getJSONObject("data");
			return Result.OK(data);
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.error("调用接口出现异常");
		}
	}


	/**
	 * 获取开关电房间
	 *
	 * @return
	 */
	@AutoLog(value = "大屏数据-获取开关电房间")
	@ApiOperation(value="大屏数据-获取开关电房间", notes="大屏数据-获取开关电房间")
	@GetMapping(value = "/roomShutOff")
	public Result<?> roomShutOff() {

		try {
			return Result.OK(bigScreenService.roomShutOff());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取开断电房间数据失败:"+error.getMessage());
		}

	}

	/**
	 * 报修维修工单统计
	 *
	 * @return
	 */
	@AutoLog(value = "大屏数据-报修维修工单统计")
	@ApiOperation(value="大屏数据-报修维修工单统计", notes="大屏数据-报修维修工单统计")
	@GetMapping(value = "/queryRepairData")
	public Result<?> queryRepairData() {

		try {
			return Result.OK(bigScreenService.queryRepairData());
		}
		catch(Exception error){
			log.error(error.getMessage(), error);
			return Result.error("获取维修工单数据失败:"+error.getMessage());
		}

	}

}
